/*
 * @Author: LVGRAPE
 * @LastEditors: LVGRAPE
 */
#include "nvic.h"

const uint8_t NVIC_InitStructure_Handle[USE_IRQn_COUNT] =
	{
		DMA1_Channel1_IRQn,

		USART1_IRQn,

		USART2_IRQn,

// [USE_USART3_IRQn]=
// {
// 	.NVIC_IRQChannel=USART3_IRQn,
// },
#if defined AT32F415RCT7 || defined AT32F413RCT7
		UART4_IRQn,

		UART5_IRQn,

#endif
		// [USE_TMR3_GLOBAL_IRQn]=
		// {
		// 	.NVIC_IRQChannel=TMR3_GLOBAL_IRQn,
		// },
		EXTI15_10_IRQn,

#if defined AT32F415RCT7
		ERTCAlarm_IRQn,

#endif // AT32F415RCT7
#if defined AT32F413RCT7
		RTCAlarm_IRQn,
#endif
		ADC1_2_IRQn,

		TMR1_TRG_HALL_TMR11_IRQn,

		TMR1_OV_TMR10_IRQn,

		// [USE_TMR1_CC_IRQn]=
		// {
		// 	.NVIC_IRQChannel=TMR1_CC_IRQn,
		// },

};
static NVIC_InitType nvicInitStrut;
void NVIC_Configuration()
{
	// total 0~15 NVIC_IRQChannelSubPriority
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

	for (uint8_t i = 0; i < USE_IRQn_COUNT; i++)
	{
		nvicInitStrut.NVIC_IRQChannel = NVIC_InitStructure_Handle[i];
		nvicInitStrut.NVIC_IRQChannelCmd = ENABLE;
		nvicInitStrut.NVIC_IRQChannelPreemptionPriority = 0;
		nvicInitStrut.NVIC_IRQChannelSubPriority = i;
		NVIC_Init(&nvicInitStrut);
	}
	// NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	// NVIC_Init(&NVIC_InitStructure);

	// NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
	// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
	// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	// NVIC_Init(&NVIC_InitStructure);

	// NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
	// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
	// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	// NVIC_Init(&NVIC_InitStructure);

	// NVIC_InitStructure.NVIC_IRQChannel = TMR3_GLOBAL_IRQn;
	// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4;
	// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	// NVIC_Init(&NVIC_InitStructure);

	// NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
	// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 5;
	// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	// NVIC_Init(&NVIC_InitStructure);

	// /* Enable the ERTC Alarm Interrupt */
	// NVIC_InitStructure.NVIC_IRQChannel = ERTCAlarm_IRQn;
	// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 6;
	// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	// NVIC_Init(&NVIC_InitStructure);

	// /* Configure and enable ADC interrupt */
	// NVIC_InitStructure.NVIC_IRQChannel = ADC1_2_IRQn;
	// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 7;
	// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	// NVIC_Init(&NVIC_InitStructure);
}
void NVIC_DISABLE_ALL(void)
{

	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
	for (uint8_t i = 0; i < USE_IRQn_COUNT; i++)
	{
		nvicInitStrut.NVIC_IRQChannel = NVIC_InitStructure_Handle[i];
		nvicInitStrut.NVIC_IRQChannelCmd = DISABLE;
		nvicInitStrut.NVIC_IRQChannelPreemptionPriority = 0;
		nvicInitStrut.NVIC_IRQChannelSubPriority = i;
		NVIC_Init(&nvicInitStrut);
	}
	SysTick->CTRL = 0;
	SysTick->VAL = 0;
	// NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	// NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
	// NVIC_Init(&NVIC_InitStructure);

	// NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
	// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
	// NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
	// NVIC_Init(&NVIC_InitStructure);

	// NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
	// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
	// NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
	// NVIC_Init(&NVIC_InitStructure);

	// NVIC_InitStructure.NVIC_IRQChannel = TMR3_GLOBAL_IRQn;
	// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4;
	// NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
	// NVIC_Init(&NVIC_InitStructure);

	// NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
	// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 5;
	// NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
	// NVIC_Init(&NVIC_InitStructure);

	// /* Enable the ERTC Alarm Interrupt */
	// NVIC_InitStructure.NVIC_IRQChannel = ERTCAlarm_IRQn;
	// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 6;
	// NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
	// NVIC_Init(&NVIC_InitStructure);

	// /* Configure and enable ADC interrupt */
	// NVIC_InitStructure.NVIC_IRQChannel = ADC1_2_IRQn;
	// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 7;
	// NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
	// NVIC_Init(&NVIC_InitStructure);
}
void NVIC_Config(uint8_t NVIC_IRQChannel, uint8_t PreemptionPriority, uint8_t SubPriority, FunctionalState newState)
{
	NVIC_InitType NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = NVIC_IRQChannel;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PreemptionPriority;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = SubPriority;
	NVIC_InitStructure.NVIC_IRQChannelCmd = newState;
	NVIC_Init(&NVIC_InitStructure);
}
